#!/bin/bash
source "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/sys_check.sh"

if [ -f /etc/os-release ]; then
   echo "Linux system."
elif [ -f /System/Library/CoreServices/SystemVersion.plist ]; then
   echo "macOS system."
   if [ -f ~/.bash_profile ]; then
      source ~/.bash_profile
    fi
elif current_os_is_windows; then
   echo "Windows system."
else
   echo "Unknown OS"
fi

# APP_INSTALL_NAME="cosyvoice"
APP_TARGET_DIR_NAME="cosyvoice"

echo "lmdprogress:3"
source "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/install_conda.sh"
source "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/conda_run.sh"
source "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/conda_and_pip_settings.sh"
export CURRENT_APP_PATH="${LMD_APPS_DIR}/${APP_TARGET_DIR_NAME}"
echo "$CURRENT_APP_PATH"
mkdir -p "$CURRENT_APP_PATH"

echo "lmdprogress:8"

if [ -d "$CURRENT_APP_PATH" ]; then
    echo "App Dir: $CURRENT_APP_PATH"
else
    echo "Error, App Dir dose not exist, please retry. $CURRENT_APP_PATH"
    exit 1
fi

echo "lmdprogress:12"

export GIT_HOST=${GITHUB_PROXY}"https://github.com"
export GIT_REPO_URL="${GIT_HOST}/FunAudioLLM/CosyVoice"
export GIT_REPO_BRANCH="main"
export LOCAL_REPO_PATH="$CURRENT_APP_PATH"
sh "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/git_clone_and_fetch.sh"

echo "lmdprogress:20"
export GIT_HOST=${GITHUB_PROXY}"https://github.com"
export GIT_REPO_URL="${GIT_HOST}/shivammehta25/Matcha-TTS.git"
export GIT_REPO_BRANCH="main"
export LOCAL_REPO_PATH="$CURRENT_APP_PATH/third_party/Matcha-TTS"

rm -rf $LOCAL_REPO_PATH
sh "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/git_clone_and_fetch.sh"

echo "lmdprogress:40"

cd "$CURRENT_APP_PATH"
CONDA_PREFIX="./venv"

if [ -d "$CONDA_PREFIX" ]; then
    echo "found ./venv. remove it. "
    rm -rf "$CONDA_PREFIX"
fi

export PYTHON_VER_ENV=3.10
conda_create_env $CONDA_PREFIX python=$PYTHON_VER_ENV -y


echo "lmdprogress:45"

function install_ffmpeg {
  source "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/install_ffmpeg.sh"
}

function check_ffmpeg_and_install_win {
  PROGRAMS=("ffmpeg" "ffprobe")
  missing_programs=0

  for PROGRAM in "${PROGRAMS[@]}"; do
    if ! command -v ${PROGRAM} >/dev/null 2>&1; then
      $PROGRAM -v
      echo "$PROGRAM not found"
      missing_programs=1
    fi
  done

  if [ $missing_programs -eq 1 ]; then
    echo "need install ffmpeg and ffprobe。"
    install_ffmpeg
  else
    echo "ffmpeg and ffprobe exist"
  fi
}
install_torch() {
    if [ -f /System/Library/CoreServices/SystemVersion.plist ]; then
        # conda_run_pip_install --pre torch torchvision torchaudio
        source "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/install_torch_mac.sh"
        conda_run_conda_forge_install ffmpeg
    elif current_os_is_windows; then
        source "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/install_torch_multi_method.sh"
        check_ffmpeg_and_install_win
    fi
}
install_torch

echo "lmdprogress:50"

conda_run_conda_forge_install pynini==2.1.5

echo "lmdprogress:55"

rm -rf requirements-temp.txt requirements-temp1.txt requirements-temp2.txt
cp requirements.txt requirements-temp.txt
grep -v "download.pytorch.org" requirements.txt > requirements-temp.txt
grep -v "torch==" requirements-temp.txt > requirements-temp1.txt
grep -v "torchaudio==" requirements-temp1.txt > requirements-temp2.txt

echo "lmdprogress:66"

conda_run_pip_install -r requirements-temp2.txt httpx==0.28.1 kaldifst==1.7.14

echo "lmdprogress:67"
conda_run_pip_install numba==0.61.2

echo "lmdprogress:68"
conda_run_pip_install huggingface_hub==0.34.6 hf_xet modelscope==1.29.2

# download models
if [ -z "$HF_ENDPOINT" ] && [ -n "$HF_MIRROR" ]; then
    export HF_ENDPOINT="$HF_MIRROR"
fi

echo "lmdprogress:80"
export MODELSCOPE_DISABLE_SYMLINKS=1

if [ -n "$HF_ENDPOINT" ]; then
    export MODELSCOPE_HOME="$CURRENT_APP_PATH/tmp_modelscope_home"
    export MODELSCOPE_CACHE="$CURRENT_APP_PATH/tmp_modelscope_cache"

    conda_run_cmd modelscope download --model iic/CosyVoice2-0.5B --local_dir pretrained_models/CosyVoice2-0.5B

    conda_run_cmd modelscope download --model iic/CosyVoice-300M --local_dir pretrained_models/CosyVoice-300M
    echo "lmdprogress:85"
    conda_run_cmd modelscope download --model iic/CosyVoice-300M-SFT --local_dir pretrained_models/CosyVoice-300M-SFT
    echo "lmdprogress:90"
    conda_run_cmd modelscope download --model iic/CosyVoice-300M-Instruct --local_dir pretrained_models/CosyVoice-300M-Instruct
    # conda_run_cmd modelscope download --model iic/CosyVoice-ttsfrd --local_dir pretrained_models/CosyVoice-ttsfrd
else
    conda_run_hf_download FunAudioLLM/CosyVoice2-0.5B --local-dir pretrained_models/CosyVoice2-0.5B
    conda_run_hf_download FunAudioLLM/CosyVoice-300M --local-dir pretrained_models/CosyVoice-300M
    echo "lmdprogress:85"
    conda_run_hf_download FunAudioLLM/CosyVoice-300M-SFT --local-dir pretrained_models/CosyVoice-300M-SFT
    echo "lmdprogress:90"
    conda_run_hf_download FunAudioLLM/CosyVoice-300M-Instruct --local-dir pretrained_models/CosyVoice-300M-Instruct
    # conda_run_hf_download FunAudioLLM/CosyVoice-ttsfrd --local-dir pretrained_models/CosyVoice-ttsfrd

    # cp "$LMD_BASE_INSTALL_SCRIPT_DIR/apps/$APP_TARGET_DIR_NAME/download_models.py" "$CURRENT_APP_PATH"
    # conda run -p $CONDA_PREFIX --no-capture-output python download_models.py
fi

# echo "lmdprogress:90"

# unzip resource.zip -d .
# conda_run_pip_install ttsfrd_dependency-0.1-py3-none-any.whl
# conda_run_pip_install ttsfrd-0.4.2-cp310-cp310-linux_x86_64.whl

echo "lmdprogress:100"


